package com.xpgk.app.login;

import com.jfinal.aop.Before;
import com.jfinal.log.Log;
import com.xpgk.app.base.AppBaseController;
import com.xpgk.dto.AjaxRender;
import com.xpgk.mvc.bentryRegister.Customer;
import com.xpgk.mvc.bentryRegister.CustomerDetail;

public class AppLoginController extends AppBaseController{
	
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(AppLoginController.class);

	private AppLoginService appLoginService;
	
	/**
	 * 登录验证
	 */
	@Before(AppLoginValidator.class)
	public void login(){
		Customer customer = getAttr("customer");
		String userName = customer.getStr(Customer.column_userName);
		String password = customer.getStr(Customer.column_password);
		String sql = getSqlMy(Customer.sqlId_selCusByUserName);
		Customer oldCustomer = Customer.dao.findFirst(sql, userName);
		if(null == oldCustomer){
			render(AjaxRender.error("该用户不存在"));
			return;
		}
		if(!oldCustomer.checkPwd(password)){
			render(AjaxRender.error("用户名或密码错误"));
			return;
		}
		if(!oldCustomer.isValid()){
			render(AjaxRender.error("您的账号已经被禁用"));
			return;
		}

		render(AjaxRender.success().addData("customer", oldCustomer));

		
	}
	
	/**
	 * 注册
	 */
	@Before(AppLoginValidator.class)
	public void regist(){
		Customer customer = getModel(Customer.class);
//		CustomerDetail cusDetail = getModel(CustomerDetail.class);
		String sql = getSqlMy(Customer.sqlId_selCusByUserName);
		Customer oldCustomer = Customer.dao.findFirst(sql, customer.getStr(Customer.column_userName));
		if(null != oldCustomer){
			render(AjaxRender.error("该手机号已注册"));
			return;
		}
		Customer cus=appLoginService.regist(customer, null);
		render(AjaxRender.success().addData("customer", cus));
	}

}
